package com.dhms.uikit

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.dhms.uikit.databinding.AlertBannerViewBinding

enum class AlertBannerType(val value: Int) {
    MESSAGE(0), ERROR(1);

    companion object {
        fun fromInt(type: Int) =
            AlertBannerType.values().associateBy(AlertBannerType::value)[type]
                ?: AlertBannerType.MESSAGE
    }
}

enum class AlertBannerTheme(val value: Int) {
    NONE(0), DARK(1), LIGHT(2);

    companion object {
        fun fromInt(type: Int) =
            AlertBannerTheme.values().associateBy(AlertBannerTheme::value)[type]
                ?: AlertBannerTheme.NONE
    }
}

class AlertBannerView : FrameLayout {

    private var bannerType = AlertBannerType.MESSAGE

    private var bannerTheme = AlertBannerTheme.DARK

    private lateinit var _binding: AlertBannerViewBinding
    val binding get() = _binding

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    )

    private fun init(context: Context, attrs: AttributeSet? = null) {
        _binding = AlertBannerViewBinding.inflate(LayoutInflater.from(context))
        setBannerStyle(attrs)
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlertBannerView)
            typedArray.recycle()
        }
        addView(_binding.root)
    }

    fun setBannerType(bannerType: AlertBannerType) {
        this.bannerType = bannerType
        setBannerStyle()
    }

    private fun setBannerStyle(attrs: AttributeSet? = null) {

        var layoutColor = 0
        var icon: Int? = null
        var iconColor = 0
        var progressBarColor = 0
        var message: String? = null
        var messageColor = 0
        var progressBarVisibility: Int? = null

        var textViewWeight = 0f
        var buttonVisibility = 0
        var buttonText: String? = null
        var buttonColor = 0


        var typedArray: TypedArray? = null
        attrs?.let {
            typedArray = context.obtainStyledAttributes(it, R.styleable.AlertBannerView)
        }

        typedArray?.let { typedArray ->
            val theme = typedArray.getInt(R.styleable.AlertBannerView_banner_theme, -1)
            if (theme != -1) {
                bannerTheme = AlertBannerTheme.fromInt(theme)
            }
            val type = typedArray.getInt(R.styleable.AlertBannerView_banner_type, 0)
            bannerType = AlertBannerType.fromInt(type)

            icon = typedArray.getResourceId(R.styleable.AlertBannerView_icon, 0)
            progressBarVisibility =
                typedArray.getInt(R.styleable.AlertBannerView_progressBar_visibility, 0)
            message = typedArray.getString(R.styleable.AlertBannerView_message)
            textViewWeight = typedArray.getFloat(R.styleable.AlertBannerView_text_view_weight, 0f)
            buttonVisibility =
                typedArray.getInt(R.styleable.AlertBannerView_button_visibility, 0)
            buttonText = typedArray.getString(R.styleable.AlertBannerView_button_text)
        }

        if (bannerType == AlertBannerType.ERROR) {
            textViewWeight = 1f
            buttonVisibility = 1
        }

        when (bannerTheme) {
            AlertBannerTheme.DARK -> {
                layoutColor = ContextCompat.getColor(context, R.color.banner_dark_layout_color)
                iconColor = ContextCompat.getColor(context, R.color.white_color)
                progressBarColor = ContextCompat.getColor(context, R.color.white_color)
                messageColor = ContextCompat.getColor(context, R.color.white_color)
                buttonColor = ContextCompat.getColor(context, R.color.white_color)
            }
            AlertBannerTheme.LIGHT -> {
                layoutColor = ContextCompat.getColor(context, R.color.banner_light_layout_color)
                iconColor = ContextCompat.getColor(context, R.color.banner_dark_layout_color)
                progressBarColor = ContextCompat.getColor(context, R.color.banner_dark_layout_color)
                messageColor = ContextCompat.getColor(context, R.color.banner_dark_layout_color)

                if (bannerType == AlertBannerType.ERROR) {
                    layoutColor =
                        ContextCompat.getColor(context, R.color.banner_light_layout_error_color)
                    iconColor = ContextCompat.getColor(context, R.color.banner_light_error_color)
                    messageColor = ContextCompat.getColor(context, R.color.banner_light_error_color)
                    buttonColor = ContextCompat.getColor(context, R.color.banner_light_error_color)
                }
            }
            else -> {

                typedArray?.let { typedArray ->
                    layoutColor = typedArray.getColor(
                        R.styleable.AlertBannerView_layout_color,
                        0
                    )
                    iconColor = typedArray.getColor(
                        R.styleable.AlertBannerView_icon_color,
                        0
                    )
                    progressBarColor = typedArray.getColor(
                        R.styleable.AlertBannerView_progressBar_color,
                        0
                    )
                    messageColor = typedArray.getColor(
                        R.styleable.AlertBannerView_message_color,
                        0
                    )
                    buttonColor = typedArray.getColor(
                        R.styleable.AlertBannerView_button_color,
                        0
                    )
                }

            }
        }

        _binding.layoutColor = layoutColor
        icon?.let {
            _binding.icon = if (it != 0) {
                context.getDrawable(it)
            } else null
        }
        _binding.iconColor = iconColor
        _binding.progressBarColor = progressBarColor
        progressBarVisibility?.let {
            _binding.progressBarVisibility = it
        }
        message?.let {
            _binding.message = it
        }
        _binding.messageColor = messageColor

        _binding.textViewWeight = textViewWeight
        _binding.buttonVisibility = buttonVisibility
        buttonText?.let {
            _binding.buttonText = it
        }
        _binding.buttonColor = buttonColor
    }
}